﻿using System.Collections.Generic;
using System.Threading;
using Gola.VN.Scb.Web.Dal;
using System.Linq;
using Gola.VN.Scb.Web.Common.Entities;
using Gola.VN.Scb.Web.Common;

namespace Gola.VN.Scb.Web.Bll
{
    public class ProductsBll
    {
        private const int NumberTryFetch = 10;

        public const int VirtualExpressionId = 99999;

        public ProductsBll(string connString)
        {
            if (!DalUtilities.ExistConnection)
            {
                DalUtilities.ConnectionString = connString;
                IsLoading = false;
            }
        }

        public bool IsLoading { set; get; }

        public bool NeedLoadData
        {
            get { return (!IsLoading) && (Products == null || Categories == null || Providers == null); }
        }

        public IEnumerable<ProductEntity> Products { private set; get; }

        public IEnumerable<CategoryEntity> Categories { private set; get; }

        public List<CategoryEntity> CategoriesAsTree { get; private set; }

        public IEnumerable<ProviderEntity> Providers { private set; get; }

        public IEnumerable<LocationEntity> ProviderLocations { private set; get; }

        public List<SearchHistoryEntity> Searchhistories { private set; get; }

        public void UpdateSearchHistory(SearchHistoryEntity dtoSearchHistory)
        {
            ProductDal.UpdateSearchHistory(dtoSearchHistory);
        }

        public void FetchData()
        {
            IsLoading = true;
            //var count = 0;
            //while (IsLoading || count > NumberTryFetch)
            //{
            //int dataStatus = ProductDal.GetSystemInfo();
            //if (dataStatus == 3)
            //{
            //    continue;
            //}
            //Reload data
            Products = ProductDal.GetAllProducts();
            Categories = ProductDal.GetAllCategores();
            Providers = ProductDal.GetAllProvider();
            ProviderLocations = ProductDal.GetAllLocations();
            Searchhistories = ProductDal.GetSearchHistories();
            //update system status to be loaded
            ProductDal.UpdateSystemInfo();
            //dispose connection
            DalUtilities.Dispose();
            //Thread.Sleep(1000 * 60 * 5);
            //count++;
            //}

            // get categories as tree
            // CategoryBiz catBiz = new CategoryBiz(); //todo: must be replace
            // CategoriesAsTree = catBiz.GetCategoriesAsTree();

            IsLoading = false;
        }

        public int InsertProvider(ProviderEntity provider)
        {
            //ProviderBiz providerBiz = new ProviderBiz();
            //provider.ExpressionId = VirtualExpressionId;
            //int result = providerBiz.InsertProvider(provider);

            //if (result > 0)
            //{
            //    List<ProviderEntity> providers = Providers.ToList();
            //    providers.Add(provider);

            //    Providers = providers;
            //}

            //return result;
            return 1;//toto: must be updated
        }

        public int UpdateProvider(ProviderEntity provider)
        {
            //ProviderBiz providerBiz = new ProviderBiz();
            //int result = providerBiz.UpdateProvider(provider);

            //if (result > 0)
            //{
            //    ProviderEntity oldProvider = Providers.FirstOrDefault(p => p.Name == provider.Name);
            //    if (oldProvider != null)
            //    {
            //        oldProvider.DisplayName = provider.DisplayName;
            //        oldProvider.Address = provider.Address;
            //        oldProvider.Email = provider.Email;
            //        oldProvider.PhoneNumber = provider.PhoneNumber;
            //        oldProvider.Logo = provider.Logo;
            //    }
            //}

            //return result;
            return 1;//todo: nust be updated
        }

        public bool InsertProduct(ProductEntity product)
        {
            try
            {
                //ProductBiz proBiz = new ProductBiz();
                //proBiz.Insert(product);

                return true;
            }
            catch
            {
                return false;
            }
            //todo: update this.Products
        }
    }
}